$(function() {
	$("#slider").slider({
		value : 1000,
		min : 0,
		max : 100000000,
		step : 1000000,
		animate : true,
		slide : function(event, ui) {
			$("#a1").val(addCommas(ui.value) + "��");
		}
	});

	$("#a1").val($("#slider").slider("value") + "��");

	$("#slider2").slider({
		value : 1,
		min : 0,
		max : 360,
		step : 1,
		animate : true,
		slide : function(event, ui) {
			$("#a2").val((ui.value) + "媛쒖썡");
		}
	});
	$("#a2").val($("#slider2").slider("value") + "媛쒖썡");

	$("#slider3").slider({
		value : 0,
		min : 0,
		max : 50,
		step : 0.01,
		animate : true,
		slide : function(event, ui) {
			$("#a3").val("��" + (ui.value) + "%");
		}
	});
	$("#a3").val("��" + $("#slider3").slider("value") + "%");

});

$(function() {
	$("#slider01").slider({
		value : 0,
		min : 0,
		max : 5000000,
		step : 1000,
		animate : true,
		slide : function(event, ui) {
			$("#b1").val(addCommas(ui.value) + "��");
		}
	});

	$("#b1").val($("#slider01").slider("value") + "��");

	$("#slider02").slider({
		value : 0,
		min : 0,
		max : 360,
		step : 1,
		animate : true,
		slide : function(event, ui) {
			$("#b2").val((ui.value) + "媛쒖썡");
		}
	});
	$("#b2").val($("#slider02").slider("value") + "媛쒖썡");

	$("#slider03").slider({
		value : 0,
		min : 0,
		max : 50,
		step : 0.01,
		animate : true,
		slide : function(event, ui) {
			$("#b3").val("��" + (ui.value) + "%");
		}
	});
	$("#b3").val("��" + $("#slider03").slider("value") + "%");

});

function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function setCheck() {

	//var repaymentType=$("input:radio[name='ch'][checked]").val();

	var repaymentType = $("input:radio[name='ch']:checked").val();
	if (repaymentType == '0') {
		$("#ss1").show();
		$("#ss2").hide();
		$("#ss3").hide();
	} else if (repaymentType == '1') {
		$("#ss1").hide();
		$("#ss2").show();
		$("#ss3").hide();
	} else {
		$("#ss1").hide();
		$("#ss2").hide();
		$("#ss3").show();
	}

}

function setCheck1() {
	var repaymentType = $("input:radio[name='ch']:checked").val();
	if (repaymentType == '0') {
		$("#sutot h4")[0].innerHTML = "�붿쟻由쎄툑��";
		$("#chtot")[0].innerHTML = "珥앸궔�낆썝湲�";
		$("#montot")[0].innerHTML = "�ㅼ닔�뱀븸";
		$("#slider01").slider({
			value : 0,
			min : 0,
			max : 5000000,
			step : 1000,
			animate : true,
			slide : function(event, ui) {
				$("#b1").val(addCommas(ui.value) + "��");
			}
		});

		$("#b1").val($("#slider01").slider("value") + "��");
	} else {
		$("#sutot h4")[0].innerHTML = "紐⑺몴湲덉븸";
		$("#chtot")[0].innerHTML = "留뚭린紐⑺몴湲덉븸";
		$("#montot")[0].innerHTML = "�붾궔�낆븸";

		$("#slider01").slider({
			value : 0,
			min : 0,
			max : 100000000,
			step : 10000,
			animate : true,
			slide : function(event, ui) {
				$("#b1").val(addCommas(ui.value) + "��");
			}
		});
		$("#b1").val($("#slider01").slider("value") + "��");
	}
}

function doCalc() {
	var AddRowCount = 0;

	// param setting
	var cost = $("#a1").val().replace(/[^0-9]/gi, "");
	//�異� 湲덉븸
	var term = $("#a2").val().replace(/[^0-9]/gi, "");
	//�異� 湲곌컙
	var interestRate = $("#a3").val().replace(/[^0-9\.]/gi, "");
	//�異� 湲덈━
	var repaymentType = $("input:radio[name='ch']:checked").val();
	var repaymentCycle = 1;

	var col = "";
	var row = new Object();
	//var row:Object;

	var totalInterest = 0;
	var avgInterest = 0;
	var totalRepayments = 0;
	var avgRepayments = 0;
	var repaymentTypeKor = "";

	var rest = cost;
	// �붽툑
	var principalSumTotal = 0;
	// �⑹엯�먭툑�⑷퀎

	var tmpRepaymentCycle = 0;	
	var repaymentCnt = (Math.floor(term / repaymentCycle) == term / repaymentCycle) ? Math.round(Math.floor(term / repaymentCycle)) : Math.round(Math.floor(term / repaymentCycle)) + 1;

	if (repaymentType == '0') {
		repaymentTypeKor = "�먮━湲덇퇏�깆긽��";
		var repayments = (cost * (interestRate / 1200) * repaymentCycle) / (1 - 1 / (Math.pow((1 + (interestRate / 1200) * repaymentCycle), term / repaymentCycle)));

		for ( i = 0; i < repaymentCnt; i++) {
			row[i] = new Object();
			row[i]["repayments"] = repayments;
			row[i]["cnt"] = i + 1;

			tmpRepaymentCycle += repaymentCycle;

			if (tmpRepaymentCycle > term) {
				//留덉�留됱씠��  = �붽툑 * (�異쒓툑由�/1200)*(�異쒓린媛�-(tmpRepaymentCycle - �곹솚二쇨린));
				row[i]["interest"] = rest * (interestRate / 1200) * (term - (tmpRepaymentCycle - repaymentCycle));

				//留덉�留� �꾩쟻�댁옄 = �꾩쟻�댁옄  + �댁옄
				totalInterest += row[i]["interest"];

				//留덉�留� �곹솚湲�  = �붽툑 + �댁옄
				row[i]["repayments"] = rest + row[i]["interest"];
			} else {
				//�댁옄怨꾩궛 = �붽툑 * (�異쒓툑由�/1200)*�곹솚二쇨린;
				row[i]["interest"] = rest * (interestRate / 1200) * repaymentCycle;

				//�댁옄 �꾩쟻 = 珥앹씠�� + �댁옄
				totalInterest += row[i]["interest"];
			}
			//�⑹엯�먭툑 = �곹솚湲� - �댁옄
			row[i]["principalSum"] = row[i]["repayments"] - row[i]["interest"];

			//�⑹엯�먭툑�⑷퀎 = �⑹엯�먭툑�⑷퀎 + �⑹엯�먭툑
			row[i]["principalSumTotal"] = principalSumTotal += row[i]["principalSum"];

			//�붽툑 = �붽툑 - �⑹엯�먭툑
			row[i]["rest"] = rest -= row[i]["principalSum"];

			// 珥� �곹솚湲�
			totalRepayments += row[i]["repayments"];
		}

	} else if (repaymentType == '1') {

		repaymentTypeKor = "�먭툑洹좊벑�곹솚";
		for ( i = 0; i < repaymentCnt; i++) {
			row[i] = new Object();
			tmpRepaymentCycle += repaymentCycle;
			row[i]["cnt"] = i + 1;

			// �댁옄 = �붽툑 * (�異쒓툑由�/1200)*�곹솚二쇨린
			if (tmpRepaymentCycle > term) {
				//留덉�留됱씠�� = �붽툑 * (�異쒓툑由�/1200)*(�異쒓린媛�-(tmpRepaymentCycle - �곹솚二쇨린));
				row[i]["interest"] = rest * (interestRate / 1200) * (term - (tmpRepaymentCycle - repaymentCycle));
				//留덉�留� �꾩쟻�댁옄 = 珥앹씠�� += �댁옄
				totalInterest += row[i]["interest"];
				//留덉�留� �곹솚湲� = �붽툑 + �댁옄
				row[i]["repayments"] = rest + row[i]["interest"];
			} else {
				//�댁옄怨꾩궛 = �붽툑 * (�異쒓툑由�/1200)*�곹솚二쇨린;
				row[i]["interest"] = rest * (interestRate / 1200) * repaymentCycle;
				//�댁옄 �꾩쟻 = 珥앹씠�� += �댁옄;
				totalInterest += row[i]["interest"];
			}

			if (repaymentCnt != i + 1) {
				//�⑹엯�먭툑 = �異쒓툑 / (�異쒓린媛� / �곹솚二쇨린)
				row[i]["principalSum"] = cost / (term / repaymentCycle);
			} else {
				row[i]["principalSum"] = rest;
			}

			//�곹솚湲� = �⑹엯�먭툑 + �붿씠��
			row[i]["repayments"] = row[i]["principalSum"] + row[i]["interest"];

			//�붽툑 = �붽툑 - �⑹엯�먭툑
			row[i]["rest"] = rest -= row[i]["principalSum"];

			//�⑹엯�먭툑�⑷퀎 = �⑹엯�먭툑�⑷퀎 + �⑹엯�먭툑
			row[i]["principalSumTotal"] = principalSumTotal += row[i]["principalSum"];

			//珥� �곹솚湲�
			totalRepayments += row[i]["repayments"];

			//�곹솚��
			//result.addItem(row);
		}

	} else if (repaymentType == '2') {
		repaymentTypeKor = "�먭툑留뚭린�쇱떆�곹솚";

		for ( i = 0; i < repaymentCnt; i++) {
			row[i] = new Object();
			row[i]["cnt"] = i + 1;

			row[i]["interest"] = cost * (interestRate / 1200) * repaymentCycle;
			
			totalInterest = row[i]["interest"] * repaymentCnt;
			if (i + 1 == repaymentCnt) {
				row[i]["principalSum"] = cost;
				//�곹솚湲� = �⑹엯�먭툑 + �붿씠��
				row[i]["repayments"] = Number(row[i]["principalSum"]) + Number(row[i]["interest"]);

			} else {
				row[i]["principalSum"] = 0;
				row[i]["repayments"] = Number(row[i]["principalSum"]) + Number(row[i]["interest"]);
			}
			//�붽툑 = �붽툑 - �⑹엯�먭툑
			row[i]["rest"] = rest -= row[i]["principalSum"];

			//�⑹엯�먭툑�⑷퀎 = �⑹엯�먭툑�⑷퀎 + �⑹엯�먭툑
			row[i]["principalSumTotal"] = principalSumTotal += row[i]["principalSum"];

			totalRepayments += row[i]["repayments"];
		}
	}

	avgInterest = Math.abs(Math.round(totalInterest / repaymentCnt));
	totalInterest = Math.abs(Math.round(totalInterest));
	avgRepayments = Math.abs(Math.round(totalRepayments / repaymentCnt));
	totalRepayments = Math.abs(Math.round(totalRepayments));

	var table1 = $("#table1 td");
	table1[0].innerHTML = cost + "��";
	// �異쒓툑��
	table1[1].innerHTML = term + "媛쒖썡";
	table1[2].innerHTML = interestRate + "%";
	table1[3].innerHTML = repaymentCycle + "媛쒖썡";
	table1[4].innerHTML = repaymentTypeKor;
	table1[5].innerHTML = addCommas(Math.round(row[0]["repayments"])) + "��";
	table1[6].innerHTML = addCommas(Math.round(totalInterest)) + "��";
	//�먭툑 洹좊벑諛⑹떇

	// 媛믪〈�ъ떆 ��젣
	if (repaymentCnt > 0) {
		$("#tbody").remove();
		$("#trbody").remove();
		$("#table2,#table1").addClass('table-bordered');
		$('<tbody id="tbody"></tbody>').appendTo("#table2");
	}

	var innerHtmlString = '<tr><td></td><td></td><td></td><td></td><td></td><td></td></tr>';
	for ( i = 0; i < repaymentCnt; i++) {
		$(innerHtmlString).appendTo("#tbody");
	}

	AddRowCount = repaymentCnt;

	var table2 = $("#table2 td");
	for ( i = 0; i < repaymentCnt; i++) {
		table2[(i * 6) + 0].innerHTML = row[i]["cnt"];													//�뚯감
		table2[(i * 6) + 1].innerHTML = addCommas(Math.round(row[i]["repayments"])) + "��";				//�곹솚湲�
		table2[(i * 6) + 2].innerHTML = addCommas(Math.round(row[i]["principalSum"])) + "��";			//�⑹엯�먭툑
		table2[(i * 6) + 3].innerHTML = addCommas(Math.round(row[i]["interest"])) + "��";				//�댁옄
		table2[(i * 6) + 4].innerHTML = addCommas(Math.round(row[i]["principalSumTotal"])) + "��";		//�⑹엯�먭툑 �⑷퀎
		table2[(i * 6) + 5].innerHTML = addCommas(Math.round(row[i]["rest"])) + "��";					//�붽툑
	}
}

function doCalc1() {
	var cost = parseFloat($("#b1").val().replace(/[^0-9]/gi, ""));				//湲덉븸
	var term = parseFloat($("#b2").val().replace(/[^0-9]/gi, ""));				//湲곌컙
	var interestRate = parseFloat($("#b3").val().replace(/[^0-9\.]/gi, ""));	//�댁쑉

	var b1 = 12 / term;
	var fond = cost * 1 * term;
	var interestResult = 0;
	var interestResult = interestRate / 100;

	var taxResult = cost * term * (term * 1 + 1) / 2 * interestResult / 12;
	var taxResult2 = cost / (term + interestResult / 12 * term * (term + 1 ) / 2 * (1 - 0 ));
	var income = taxResult + fond;

	var repaymentType = $("input:radio[name='ch']:checked").val();

	if (repaymentType == '0') {

		$("#fond")[0].innerHTML = addCommas(fond) + "��";								// �먭툑
		$("#term")[0].innerHTML = term + "媛쒖썡(" + Math.round(term / 12) + "��)";		// 湲곌컙
		$("#income")[0].innerHTML = addCommas(Math.round(income)) + "��";				//�ㅼ닔�뱀븸
		$("#interestRate")[0].innerHTML = addCommas(interestRate) + "%";				//�댁쑉
		$("#interest")[0].innerHTML = addCommas(Math.round(taxResult)) + "��";			//�댁옄
		$("#tax")[0].innerHTML = addCommas(0) + "��";									//�멸툑
		$("#intersys")[0].innerHTML = "�⑤━";

		$("#income_1")[0].innerHTML = addCommas(Math.round(income - (Math.round(taxResult * 0.095)))) + "��";		//�ㅼ닔�뱀븸
		$("#income_2")[0].innerHTML = addCommas(Math.round(income - (Math.round(taxResult * 0.154)))) + "��";		//�ㅼ닔�뱀븸

		$("#interest_1")[0].innerHTML = addCommas(Math.round(taxResult - (Math.round(taxResult * 0.095)))) + "��";		//�댁옄
		$("#interest_2")[0].innerHTML = addCommas(Math.round(taxResult - (Math.round(taxResult * 0.154)))) + "��";		//�댁옄

		$("#tax_1")[0].innerHTML = addCommas(Math.round(taxResult * 0.095)) + "��";		//�멸툑
		$("#tax_2")[0].innerHTML = addCommas(Math.round(taxResult * 0.154)) + "��";		//�멸툑

		$("#fond_1")[0].innerHTML = addCommas(fond) + "��";								// �먭툑
		$("#term_1")[0].innerHTML = term + "媛쒖썡(" + Math.round(term / 12) + "��)";		// 湲곌컙
		$("#interestRate_1")[0].innerHTML = addCommas(interestRate) + "%";				//�댁쑉

	} else {		
		intRest = term * (term + 1) / 2 * interestResult / 12;

		t1 = cost / (term + interestResult / 12 * term * (term + 1 ) / 2 * (1 - 0));
		t2 = cost / (term + interestResult / 12 * term * (term + 1 ) / 2 * (1 - 0.095));
		t3 = cost / (term + interestResult / 12 * term * (term + 1 ) / 2 * (1 - 0.154));

		int_after1 = t1 * interestResult / 12 * term * (term + 1) / 2;
		int_after2 = t2 * interestResult / 12 * term * (term + 1) / 2;
		int_after3 = t3 * interestResult / 12 * term * (term + 1) / 2;

		tax_after2 = int_after2 * (1 - 0.095 );
		tax_after2 = int_after2 - tax_after2;

		tax_after3 = int_after3 * (1 - 0.154 );
		tax_after3 = int_after3 - tax_after3;
		
		$("#fond")[0].innerHTML = addCommas(cost) + "��";						// �먭툑
		$("#interestRate")[0].innerHTML = addCommas(interestRate) + "%";
		$("#term")[0].innerHTML = term + "媛쒖썡(" + Math.round(term / 12) + "��)";		// 湲곌컙

		$("#tax")[0].innerHTML = addCommas(0) + "��";	 //�멸툑
		$("#tax_1")[0].innerHTML = addCommas(Math.round(tax_after2)) + "��";		//�멸툑
		$("#tax_2")[0].innerHTML = addCommas(Math.round(tax_after3)) + "��";		//�멸툑

		$("#income")[0].innerHTML = addCommas(Math.round(t1)) + "��";		//�ㅼ닔�뱀븸
		$("#income_1")[0].innerHTML = addCommas(Math.round(t2)) + "��";		//�ㅼ닔�뱀븸
		$("#income_2")[0].innerHTML = addCommas(Math.round(t3)) + "��";		//�ㅼ닔�뱀븸

		$("#interest")[0].innerHTML = addCommas(Math.round(int_after1)) + "��";			//�댁옄
		$("#interest_1")[0].innerHTML = addCommas(Math.round(int_after2)) + "��";		//�댁옄
		$("#interest_2")[0].innerHTML = addCommas(Math.round(int_after3)) + "��";		//�댁옄
	}
		
		
	
		
}

function reset(){
	$("#a1").val("0");
	$("#a2").val("0");
	$("#a3").val("0");

	$("#slider").slider("value", 0);
	$("#slider2").slider("value", 0);
	$("#slider3").slider("value", 0);

	$("#table1 tbody tr td").html("");
	$("#tbody").remove();
	$("#trbody").remove();
	$('<tbody id="tbody"></tbody>').appendTo("#table2");
}

function reset1(){
	$("#b1, #b2, #b3").val("0");
	$("#slider01, #slider02, #slider03").slider("value", 0);	
	$("#tax, #tax_1, #tax_2, #income, #income_1, #income_2, #interest, #interest_1, #interest_2, #fond, #interestRate, #term, #intersys").html('');
}

function appendDate(obj, inputId, sliderId) {
	var u = parseInt($(obj).val());
	var t1 = parseInt($('#' + inputId).val().replace(/\,/g, ""));

	if (obj.value == '0') {
		$('#' + inputId).val(addCommas(u) + "媛쒖썡");
		$('#' + sliderId).slider('value', u);
	} else {
		$('#' + inputId).val(addCommas(t1 + u) + "媛쒖썡");
		$('#' + sliderId).slider('value', t1 + u);
	}
}

function appendMn(obj, inputId, sliderId) {
	var u = parseInt($(obj).val());
	var t1 = parseInt($('#' + inputId).val().replace(/\,/g, ""));

	if (obj.value == '0') {
		$('#' + inputId).val(addCommas(u) + "��");
		$('#' + sliderId).slider('value', u);
	} else {
		$('#' + inputId).val(addCommas(t1 + u) + "��");
		$('#' + sliderId).slider('value', t1 + u);
	}
}    